﻿/*
 *  PirateShare - a tool for fair&legal content sharing among friends.  
 *  see <http://pirateshare.codeplex.com> for latest version.
 *
 *  Copyright (C) 2010 Ilya Elenskiy
 *
 *  This program is free software; you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation; either version 2 of the License, or 
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but 
 *  WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License 
 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Timers;
using System.IO;

namespace PirateShare
{
    class IndexWorker
    {
        public volatile int Progress = -1;
        private List<string> FilesToScan;

        public bool interruptWork;

        public void runIndex()
        {
            lock (this)
            {
                interruptWork = false;
                Progress = -1;
            }

            List<string> Folders;
            List<LocalSharedFile> Files;

            lock (Globals.ThisUser)
            {
                Folders = new List<string>(Globals.ThisUser.SharedFolders);
                Files = new List<LocalSharedFile>(Globals.ThisUser.SharedFiles);
            }

            //make index of files to check
            FilesToScan = new List<string>();

            foreach (string folder in Folders)
            {
                ScanFolder(folder);
            }

            //delete obsolete local files from old index
            List<LocalSharedFile> ActualFiles = new List<LocalSharedFile>();

            foreach (LocalSharedFile f in Files)
            {
                if (FilesToScan.Contains(f.FullFilename))
                {
                    //check if the file has been modified since, add only if not
                    FileInfo oldFileInfo = new FileInfo(f.FullFilename);
                    if (oldFileInfo.LastWriteTimeUtc == f.LastModified)
                    {
                        ActualFiles.Add(f);
                        FilesToScan.Remove(f.FullFilename);
                    }
                }
            }


            //index new or modified files
            int finished = 0;

            foreach (string f in FilesToScan)
            {
                ActualFiles.Add(new LocalSharedFile(f));
                finished++;
                lock (this)
                {
                    Progress = (int)Math.Round((double)finished / (double)FilesToScan.Count * 100.0);

                    if (this.interruptWork)
                    {
                        return;
                    }
                }
            }

            

            lock (Globals.ThisUser)
            {
                Globals.ThisUser.SharedFiles = ActualFiles;
                Globals.SaveUser();
            }
        }

        private void ScanFolder(string folder)
        {
            FilesToScan.AddRange(Directory.GetFiles(folder));

            foreach (string dir in Directory.GetDirectories(folder))
            {
                lock (this)
                {
                    if (this.interruptWork)
                    {
                        return;
                    }
                }
                ScanFolder(dir);
            }
        }
    }
}
